import java.util.Collections;


public class SolutionStateList extends StateList {
	private StringBuffer solutionInstructionString;
	private int currentInstructionIndex = 0;
	private boolean isReady = false;
	
	public SolutionStateList() {
		super();
	}
	
	public void finalizeSolution() {
		Collections.reverse(this);
		isReady = true;
	}
	
	public void setReady() {
		isReady = true;
	}
	
	public StringBuffer getInstructionString() {
		return solutionInstructionString;
	}
	
	public int getCurrentInstructionIndex() {
		return currentInstructionIndex;
	}
	
	public void setSolutionInstructionString(StringBuffer instructionString) {
		solutionInstructionString = instructionString;
	}
	
	public void printSolutionString() {
		System.out.println(solutionInstructionString.toString());
	}
	
	public void addAllAtBeginning(StateList stateList) {
		StateList newS = new StateList();
		this.ensureCapacity((this.size() + stateList.size()));
		newS.addAll(this);
		this.clear();
		this.addAll(stateList);
		this.addAll(newS);
	}
	
	public char getCurrentInstruction() {
		if ((solutionInstructionString == null)||(!isReady)) return 'x';
		if ((currentInstructionIndex < 0)||(currentInstructionIndex >= solutionInstructionString.length())) return 'x';
		return solutionInstructionString.charAt(currentInstructionIndex);
	}
	
	public void reverseInstructionString() {
		solutionInstructionString.reverse();
	}
	
	public char getNextInstruction() {
		if ((solutionInstructionString == null)||(!isReady)) return 'x';
		
		currentInstructionIndex++;

		if (currentInstructionIndex >= solutionInstructionString.length()) {
			currentInstructionIndex = -1;
			this.setCurrentStateIndex(0);
			return 'x';
		} else {
			char instruction = solutionInstructionString.charAt(currentInstructionIndex);

		}
		return solutionInstructionString.charAt(currentInstructionIndex);
	}
	
	public char getPrevInstruction() {
		if ((solutionInstructionString == null)||(!isReady)) return 'x';
		
		currentInstructionIndex--;

		if (currentInstructionIndex < 0) {
			currentInstructionIndex = solutionInstructionString.length();
			this.setCurrentStateIndex(this.size()-1);
			return 'x';
		} else {
			char instruction = solutionInstructionString.charAt(currentInstructionIndex);

		}
		return solutionInstructionString.charAt(currentInstructionIndex);
	}

	public void addInstructionsAtBeginning(StringBuffer newInstructions) {
		if (newInstructions!=null) {
			if (solutionInstructionString==null) solutionInstructionString = new StringBuffer();
			
			solutionInstructionString.insert(0, newInstructions);
		}
	}
}
